﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;

namespace HUST.JackRiver.Demo.Doc2Text
{
    internal class Document
    {
        protected string filePath; //存储文件路径

        internal Document(string FilePath)
        {
            filePath = FilePath;
        }

        internal virtual PlainText ToPlainText()
        {
            return null;
        }
    }


    internal class PDFDocument : Document
    {
        internal PDFDocument(string FilePath)
            : base(FilePath)
        {
        }

        internal override PlainText ToPlainText()
        {
            PlainText result = null;
            org.pdfbox.pdmodel.PDDocument doc=null;
            org.pdfbox.util.PDFTextStripper pdfStripper=null;
            try
            {
                doc = org.pdfbox.pdmodel.PDDocument.load(filePath);  //加载PDF文件
                pdfStripper = new org.pdfbox.util.PDFTextStripper(); //创建PDF剥离器实例
                string content = pdfStripper.getText(doc);//获取文本
                result = new PlainText(content, "", true);
            }
            catch (Exception e)
            {
                result = new PlainText("", e.Message, false);
            }
            finally
            {
                doc.close();
            }
            return result;
        }
    }

    internal class DocDocument : Document
    {
        internal DocDocument(string FilePath)
            : base(FilePath)
        {
        }


        internal override PlainText ToPlainText()
        {
            PlainText result=null;
            Microsoft.Office.Interop.Word.ApplicationClass app = null;
            Microsoft.Office.Interop.Word.Document doc = null;
            try
            {
                object missing = System.Reflection.Missing.Value;
                object FileName = filePath;
                object readOnly = true;
                object isVisible = false;


                app = new Microsoft.Office.Interop.Word.ApplicationClass();
                doc = app.Documents.Open(ref FileName, ref missing, ref readOnly,
                    ref missing, ref missing, ref missing, ref missing, ref missing,
                    ref missing, ref missing, ref missing, ref isVisible, ref missing,
                    ref missing, ref missing, ref missing);  //打开文档

                string text = "";

                //读取全部内容
                text = doc.Content.Text.Trim();

                text = text.Replace("\a", " ");    //替换空串为空。（word中\a代表空串，但在C#中，代表响铃）否则显示控制台程序时会响
                text = text.Replace("\r", "\n");    //替换回车为回车换行
                result = new PlainText(text, "", true);
                doc.Close(missing, missing, missing);
            }
            catch (Exception e)
            {
                result = new PlainText("", e.Message, false);
            }
            finally
            {
                if (app != null)
                {
                    app.Quit();
                }
            }
            return result;
        }
    }

    internal class XlsDocument : Document
    {
        internal XlsDocument(string FilePath)
            : base(FilePath)
        {
        }


        internal override PlainText ToPlainText()
        {
            PlainText result = null;
            Microsoft.Office.Interop.Excel.ApplicationClass app = null;
            Microsoft.Office.Interop.Excel.Workbook doc = null;
            try
            {
                object missing = System.Reflection.Missing.Value;
                object readOnly = true;

                app = new Microsoft.Office.Interop.Excel.ApplicationClass();
                doc = app.Workbooks.Open(filePath, missing, readOnly, missing, missing, missing,
                    missing, missing, missing, missing, missing, missing, missing, missing, missing);

                string text = "";


                //读取全部内容

                Microsoft.Office.Interop.Excel.Worksheet ews;//定义worksheet变量

                int iRowCnt = 0, iColCnt = 0;

                int iEWSCnt = doc.Worksheets.Count;

                for (int m = 1; m <= iEWSCnt; m++)//遍历excel表格
                {
                    ews = (Microsoft.Office.Interop.Excel.Worksheet)doc.Worksheets[m];
                    iRowCnt = ews.UsedRange.Cells.Rows.Count;//行的数目
                    iColCnt = ews.UsedRange.Cells.Columns.Count;//列的数目
                    string temp = "";
                    for (int i = 1; i <= iRowCnt; i++)
                    {
                        for (int j = 1; j <= iColCnt; j++)
                        {
                            temp = ((Microsoft.Office.Interop.Excel.Range)ews.UsedRange.Cells[i, j]).Text.ToString();//遍历所有单元格，然后添加单元格内容
                            text += temp;
                            text += temp == "" ? "" : "\n";//非空时加入换行
                        }
                    }
                }
                result = new PlainText(text==""?"<空文档！>":text, "", true);
                doc.Close();
            }
            catch (Exception e)
            {
                result = new PlainText("", e.Message, false);
            }
            finally
            {
                if (app != null)
                {
                    app.Quit();
                }
            }
            return result;
        }
    }

    internal class PPTDocument : Document
    {
        internal PPTDocument(string FilePath)
            : base(FilePath)
        {
        }


        internal override PlainText ToPlainText()
        {
            PlainText result = null;
            Microsoft.Office.Interop.PowerPoint.ApplicationClass app = null;
            Microsoft.Office.Interop.PowerPoint.Presentation presentation = null;
            try
            {

                app = new Microsoft.Office.Interop.PowerPoint.ApplicationClass();
                presentation = app.Presentations.Open(filePath, Microsoft.Office.Core.MsoTriState.msoCTrue, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoFalse);

                string text = "";
                string temp = "";
                //读取全部内容
                foreach (Microsoft.Office.Interop.PowerPoint.Slide slide in presentation.Slides)//遍历每一个slide（幻灯页）
                {
                    foreach (Microsoft.Office.Interop.PowerPoint.Shape shape in slide.Shapes)//遍历每一个shape
                    {
                        if (shape.Type == Microsoft.Office.Core.MsoShapeType.msoTextBox)
                            //IMPORTANT:必须加上这个判断，以便只读取文字
                        {
                            temp = shape.TextFrame2.TextRange.Text;//读取textframe（文本帧）
                            text += temp;
                            text += temp == "" ? "" : "\n";//非空时键入空行
                        }
                    }
                }
                result = new PlainText(text, "", true);
                presentation.Close();
            }
            catch (Exception e)
            {
                result = new PlainText("", e.Message, false);
            }
            finally
            {
                if (app != null)
                {
                    app.Quit();
                }
            }
            return result;
        }
    }
}
